package com.momo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Kagome on 2019/5/21.
 *  https://blog.csdn.net/qq_38746645/article/details/77095058
 *  https://cloud.tencent.com/developer/article/1345959
 */
public class ScalLoginController {
    String WX_SCAN_CODE_URL = "https://open.weixin.qq.com/connect/qrconnect?appid={APPID}&redirect_uri={REUTL}&response_type=code&scope=snsapi_login&state={STATE}#wechat_redirect";
    // 千万要记住，这个是微信开放平台的APPID
    String WX_PLATFROM_APPID = "XXXXXX";
    // 你的回调地址
    String scanReUrl = "http://你的网址/user/wxLoginCallback";

    /**
     * 微信扫码登陆
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "weixinScanLogin", method = RequestMethod.GET)
    public void weixinRetrun(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 获取回调url(非必填，只是附带上你扫码之前要进入的网址，具体看业务是否需要)
        String url = request.getParameter("reurl");
        // 拼接扫码登录url
        String wxLoginurl = WX_SCAN_CODE_URL;
        wxLoginurl = wxLoginurl.replace("{APPID}", WX_PLATFROM_APPID).replace("{REUTL}", scanReUrl).replace("{STATE}",
                url);
        wxLoginurl = response.encodeURL(wxLoginurl);
        response.sendRedirect(wxLoginurl);
    }
}
